let {
    getDataController,
    addUserController,
    userFindByIdController,
    delDataController,
    updateDataController,
    getDataUnOpenController
} = require('../controller/publicController');

// 通用的数据加载方法
getDateService = async (req, res, next) => {
    //  解构到用户输入数据 pageIndex,当前页 pageSize 每页显示多少条
    let { role, pageIndex, pageSize } = req.query;
    //  判断参数是否合理
    if (role && pageIndex && pageSize && pageIndex > 0 && pageSize > 0) {
        let results = await getDataController(role, { path: "classId subjectId" }, pageIndex, pageSize)
        // 判断是否存在数据
        if (results.results.length) {
            res.json({
                code: 200,
                message: "查询数据成功",
                total: results.total,
                pageCount: results.pageCount,
                data: results.results

            })
        } else {
            res.json({
                code: 201,
                message: "查询数据失败,没有数据",
            })
        }
    } else {
        res.json({
            code: 201,
            message: "查询数据失败,参数不正确",
        })
    }
}
// 通用的用户新增方法
addDataService = async (req, res, next) => {
    let { role, userNumber, name, gender, age, phone} = req.body;
    if (role && userNumber && name && gender && age && phone && classId) {
        let idResults = await userFindByIdController(role, userNumber)
        if (idResults.results) {
            res.json({
                code: 201,
                message: "数据添加失败,改学员已经存在"
            })
        } else {
            let results = await addUserController(role, userNumber, name, gender, age, phone, classId);
            if (results.results) {
                res.json({
                    code: 200,
                    message: "数据添加成功"
                })
            } else {
                res.json({
                    code: 201,
                    message: "数据添加失败,未知原因"
                })
            }
        }
    } else {
        res.json({
            code: 201,
            message: "数据添加失败,参数不正确"
        })
    }
}

delDataService = async (req, res,) => {
    let { role, userNumber } = req.body;
    if (role && userNumber) {
        let idResults = await userFindByIdController(role, userNumber);
        if (idResults.results) {
            let results = await delDataController(role, userNumber)
            if (results.results?.deletedCount == 1) {
                res.json({
                    code: 200,
                    message: "数据删除成功"
                })
            } else {
                res.json({
                    code: 201,
                    message: "数据删除失败，未知异常"
                })
            }

        } else {
            res.json({
                code: 201,
                message: "数据删除失败，该数据不存在"
            })
        }
    } else {
        res.json({
            code: 201,
            message: "数据删除失败，参数不正确"
        })
    }
}

updateDataService = async (req, res,) => {
    let { role, userNumber, name, gender, age, phone } = req.body;
    if (role && userNumber && name && gender && age && phone ) {
        let idResults = await userFindByIdController(role, userNumber);
        if (idResults.results) {
            let results = await updateDataController(role, userNumber, name, gender, age, phone)
            if (results.results?.matchedCount == 1) {
                res.json({
                    code: 200,
                    message: "数据信息修改成功"
                })
            } else {
                res.json({
                    code: 201,
                    message: "数据信息修改失败，未知异常"
                })
            }

        } else {
            res.json({
                code: 201,
                message: "数据信息修改失败，该数据不存在"
            })
        }
    } else {
        res.json({
            code: 201,
            message: "数据信息修改失败，参数不正确"
        })
    }
}

getDataUnOpenService = async (req, res, next) => {
    //  解构到用户输入数据 pageIndex,当前页 pageSize 每页显示多少条
    let { role, pageIndex, pageSize } = req.query;
    //  判断参数是否合理
    if (role && pageIndex && pageSize && pageIndex > 0 && pageSize > 0) {
        let results = await getDataUnOpenController(role, { path: "classId subjectId" }, pageIndex, pageSize)
        // 判断是否存在数据
        if (results.results.length) {
            res.json({
                code: 200,
                message: "查询数据成功",
                pageCount: results.pageCount,
                data: results.results,
                total: results.total
            })
        } else {
            res.json({
                code: 201,
                message: "查询数据失败,没有数据",
            })
        }
    } else {
        res.json({
            code: 201,
            message: "查询数据失败,参数不正确",
        })
    }
}



module.exports = {
    getDateService,
    addDataService,
    delDataService,
    updateDataService,
    getDataUnOpenService
}